<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/25
 * Time: 22:59
 */

namespace app\admin\controller;

use app\admin\Controller;
use think\Session;
use think\Db;

class Cates extends Controller
{

    public function index()
    {
        $params = $this->request->param();
        $maps = [];
        if(isset($params['name'])&&$params['name']!=''){
            $maps['name'] = array('like','%'.trim($params['name']).'%');
        }


        if(isset($params['pid']) && $params['pid']==''){
            $maps['pid'] = array('neq',0);
        } elseif(isset($params['pid']) && $params['pid']>=0) {
            $maps['pid'] = array('eq',intval($params['pid']));
        }

        //所有顶级分类
        $topcate = Db::table('kyf_cates')->where('pid=0')->field('cate_id,name')->select();
        $top = array();
        foreach ($topcate as $item){
            $id = $item['cate_id'];
            $top[$id] = $item['name'];
        }

        // 分页查询 每页显示10条
        $cates = Db::table('kyf_cates')->where($maps)->order('cate_id desc')->paginate(10,false,$this->request->param());
        $res = $cates->toArray();
        $pages = $cates->render();

        foreach ($res['data'] as &$item) {
            $item['topname'] = $item['pid']>0 ? $top[$item['pid']] : '';
        }

        $this->view->assign('list',$res['data']);
        $this->view->assign('total',$res['total']);
        $this->view->assign('topcates',$topcate);
        $this->view->assign('page',$pages);

        return $this->view->fetch();
    }

    /**
     * 添加商品分类
     * @return mixed
     */
    public function add()
    {
        if($this->request->isPost()){
            $params = $this->request->param();

            $name = isset($params['name'])?trim($params['name']):'';
            $pid = intval($params['pid']);
            $pic = isset($params['pic'])?trim($params['pic']):'#';
            if($name==''){
                exit('参数错误');
            }

            $data['name'] = $name;
            $data['pid'] = $pid;
            $data['pic'] = $pic;

            $cateid = Db::table('kyf_cates')->insert($data,false,true);
            if($cateid > 0){
                return ajax_return(array(),'添加成功');
            } else {
                return ajax_return(array(),'提交失败');
            }

        } else {
            $topcates = Db::table('kyf_cates')->field('cate_id,name')->where('pid=0')->select();
            $this->view->assign('topcates',$topcates);
            return $this->view->fetch();
        }
    }


    /**
     * 删除商品分类
     */
    public function del(){

        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            return ajax_return(array('code'=>1,'msg'=>'参数错误'));
        }
        $rt = Db::table('kyf_cates')->where('cate_id='.$id)->delete();
        if($rt){
            return ajax_return(array(),'删除成功');
        } else {
            return ajax_return(array(),'删除失败',1);
        }
    }

    /**
     * 修改商品分类
     */
    public function update(){
        $params = $this->request->param();
        $id  = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            exit('参数错误');
        }

        if($this->request->isPost()){
            $params = $this->request->param();
            $name = isset($params['name'])?trim($params['name']):'';
            $pid = intval($params['pid']);
            $pic = isset($params['pic'])?trim($params['pic']):'#';
            if($name==''){
                exit('参数错误');
            }

            $data['name'] = $name;
            $data['pid'] = $pid;
            $data['pic'] = $pic;

            $srt = Db::table('kyf_cates')->where('cate_id='.$id)->update($data);
            if($srt){
                return ajax_return(array(),'修改成功');
            } else {
                return ajax_return(array(),'修改失败');
            }

        } else {

            $cate = Db::table('kyf_cates')->where('cate_id='.$id)->find();
            $topcates = Db::table('kyf_cates')->field('cate_id,name')->where('pid=0')->select();

            $this->view->assign('cate',$cate);
            $this->view->assign('topcates',$topcates);
            return $this->view->fetch();
        }

    }

}